package com.firebird.app.kotlin.stock.trend.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.firebird.app.kotlin.stock.trend.bean.Stock
import com.firebird.app.kotlin.stock.trend.view.HVListView
import com.firebird.app.kotlin.stock.trend.R


/**
 * 沪深A股列表数据适配器
 * @author ljk
 * @since  2019年4月18日
 */
class StockRankDataAdapter(context: Context, hvlistView: HVListView) : BaseAdapter() {
    private var stockList: List<Stock>? = null
    private val mInflater: LayoutInflater
    private val hvlistView: HVListView
    fun setData(stockList: List<Stock>?) {
        this.stockList = stockList
    }

    override fun getCount(): Int {
        return if (stockList != null) {
            stockList!!.size
        } else 0
    }

    override fun getItem(position: Int): Any? {
        return if (stockList != null) {
            stockList!![position]
        } else null
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView
        var holder: ViewHolder? = null
        if (convertView == null) {
            holder = ViewHolder()
            convertView = mInflater.inflate(R.layout.item_hvlistview, parent, false)
            holder.tv_name = convertView.findViewById<View>(R.id.name) as TextView
            holder.tv_code = convertView.findViewById<View>(R.id.code) as TextView
            holder.tv_now = convertView.findViewById<View>(R.id.item1) as TextView
            holder.tv_low = convertView.findViewById<View>(R.id.item2) as TextView
            holder.tv_high = convertView.findViewById<View>(R.id.item3) as TextView
            holder.tv_open = convertView.findViewById<View>(R.id.item4) as TextView
            holder.tv_close = convertView.findViewById<View>(R.id.item5) as TextView
            holder.tv_volume = convertView.findViewById<View>(R.id.item6) as TextView
            holder.tv_amount = convertView.findViewById<View>(R.id.item7) as TextView
            convertView.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
        }
        if (stockList != null && stockList!!.size > 0) {
            val stock = stockList!![position]
            holder.tv_name!!.text = stock.name
            holder!!.tv_code!!.text = stock.code
            holder.tv_now!!.text=stock.nowPrice.toString()
            holder.tv_low!!.text = stock.getlPrice().toString() + ""
            holder.tv_high!!.text = stock.gethPrice().toString() + ""
            holder.tv_open!!.text=stock.openPrice.toString()
            holder.tv_close!!.text=stock.closePrice.toString()
            holder.tv_volume!!.text = stock.traVolume.toString() + ""
            holder.tv_amount!!.text = stock.traAmount.toString() + ""
        }
        /*for (int i = 0; i < 7; i++) {
            ((TextView) convertView.findViewById(R.id.item1 + i)).setText("数据" + position + "行" + (i + 2) + "列");
        }*/
//校正（处理同时上下和左右滚动出现错位情况）
/*View child = ((ViewGroup) convertView).getChildAt(1);
        int headscrollx = hvlistView.getHeadScrollX();
        if (child.getScrollX() != headscrollx) {
            child.scrollTo(hvlistView.getHeadScrollX(), 0);
        }*/return convertView!!
    }

    /**
     * 用于保存ListView中重用的item视图的引用
     */
    internal inner class ViewHolder {
        var tv_name: TextView? = null
        var tv_code: TextView? = null
        var tv_now: TextView? = null
        var tv_low: TextView? = null
        var tv_high: TextView? = null
        var tv_open: TextView? = null
        var tv_close: TextView? = null
        var tv_volume: TextView? = null
        var tv_amount: TextView? = null
    }

    init {
        mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        this.hvlistView = hvlistView
    }
}